package com.coolcode.common;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class HttpLogFormatter {

    public static String logRequest(HttpServletRequest request, Object body) {
        if (request != null) {
            StringBuilder builder = new StringBuilder();
            Map<String, String> parameters = buildParametersMap(request);
            builder.append("REQUEST ");
            builder.append("method=[").append(request.getMethod()).append("]");
            builder.append("path=[").append(request.getRequestURI()).append("]");
            builder.append("headers=[").append(buildHeaderMap(request)).append("]");

            if (!parameters.isEmpty()) {
                builder.append("parameters=[").append(parameters).append("]");
            }

            if (body != null) {
                builder.append("body=[" + body + "]");
            }
            return builder.toString();
        } else {
            return "Invalid parameter in logRequest";
        }
    }

    public static String logResponse(HttpServletRequest request, HttpServletResponse response, Object body) {
        if (request != null && response != null && body != null) {
            StringBuilder builder = new StringBuilder();
            builder.append("RESPONSE ");
            builder.append("status=[").append(response.getStatus()).append("]");
            builder.append("method=[").append(request.getMethod()).append("]");
            builder.append("path=[").append(request.getRequestURI()).append("]");
            builder.append("responseHeaders=[").append(buildHeaderMap(response)).append("]");
            builder.append("responseBody=[").append(body).append("]");

            return builder.toString();
        } else {
            return "Invalid parameter in logRequest";
        }
    }


    private static Map<String,String> buildParametersMap(HttpServletRequest request) {
        HashMap<String, String> resultMap = new HashMap<>();
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String key = parameterNames.nextElement();
            String value = request.getParameter(key);
            resultMap.put(key, value);
        }
        return resultMap;
    }

    private static HashMap<String, String> buildHeaderMap(HttpServletRequest request) {
        HashMap<String, String> resultMap = new HashMap<>();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = headerNames.nextElement();
            String value = request.getHeader(key);
            resultMap.put(key, value);
        }
        return resultMap;
    }

    private static Map<String,String> buildHeaderMap(HttpServletResponse response) {

        HashMap<String, String> resultMap = new HashMap<>();
        Collection<String> headerNames = response.getHeaderNames();

        for (String headerName : headerNames) {
            resultMap.put(headerName, response.getHeader(headerName));
        }
        return resultMap;
    }
}
